/*
 * CAN.h
 *
 * Created: 04.10.2012 11:34:54
 *  Author: bardbakk
 */ 

/*!
 * @header		CAN.h
 * @discussion	This file contains all declarations for CAN communication
*/		

#ifndef CAN_H_
#define CAN_H_

#include <stdint.h>



/*!
 * @discussion	CAN IDs
*/		
#define CAN_TRACK_CONTROL_MSG 101
#define CAN_GOAL_MSG 3
#define CAN_READ_PI_EEPROM_MSG 4
#define CAN_CHANGE_PI_MSG 5
#define CAN_REQUEST_PI_MSG 6
#define CAN_SRAM_TEST_MSG 7
#define CAN_PLAY_MSG 8
#define CAN_RESUME_PLAY_MSG 9
#define CAN_TRACK_MSG 10
#define CAN_LEDS_DEC_MSG 11
#define CAN_LEDS_INIT_MSG 12


/*!
 * @typedef		CAN_message_t
 * @discussion	A struct containing ID, length and 8 bytes of data.
*/ 	
typedef struct{
	unsigned int id;
	uint8_t length;
	int8_t data[8];
} CAN_message_t;


/*!
 * @var			CAN_msg_rcvd
 * @discussion	A boolean which tells if there's an CAN message ready to be received. Set by the interrupt routine when an RX interrupt is invoked. 
*/ 		
volatile uint8_t CAN_msg_rcvd;


/*!
 * @function	CAN_init.
 * @discussion	Initializes the CAN controller in normal mode with no ID filter.
				Enables receive interrupts on falling edge.
 * @result		None.
 */		
void CAN_init();


/*!
 * @function	CAN_message_send.
 * @discussion	Sends a struct containing ID, length and data over CAN.
 * @param		msg		The struct to be sent.
 * @result		None.
 */		
void CAN_message_send(CAN_message_t msg);


/*!
 * @function	CAN_message_receive.
 * @discussion	Receives a struct containing ID, length and data over CAN.
 * @result		The received struct.
 */	
CAN_message_t* CAN_message_receive();


#endif /* CAN_H_ */